<#assign classNameLower = className?uncap_first>
package ${packageName}.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.util.WebUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;

import com.shareinfo.base.pagination.PageInfo;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
<#include "/controller_import.include">

@Controller
@Scope("prototype")
@RequestMapping(value = "${urlPath}/${classNameLower}")
public class ${className}Controller {
	
  @Resource(name = "${classNameLower}Service")
  private ${className}Service ${classNameLower}Service;
  
  @ModelAttribute
	public ${className}Model get(@RequestParam(required=false) ${table.keyJavaType} id) {
		if (id!=null){
			return ${classNameLower}Service.getById(id);
		}else{
			return new ${className}Model();
		}
	}
  
	/** 
	 * 列出对象
	**/
	@RequestMapping(value = {"listForEasyui"})
	@ResponseBody
	public Map<String, Object> listForEasyui(${className}Query ${classNameLower}Query,HttpServletRequest request,HttpServletResponse response) {
		PageInfo<${className}Model> pageInfo = ${classNameLower}Service.selectList(${classNameLower}Query);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(PageInfo.EASYUI_ROWS, pageInfo.getList());
		map.put(PageInfo.EASYUI_TOTAL, pageInfo.getTotalRecord());
		return map;
	}
	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = {"showForEasyui"})
	@ResponseBody
	public Map<String, Object> showForEasyui(HttpServletRequest request,HttpServletResponse response) throws Exception {
		<@generateIdParameter/>
		${className}Model ${classNameLower} = (${className}Model)${classNameLower}Service.getById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("${classNameLower}", ${classNameLower});
		return map;
	}
	
	/** 
	 * 进入新增页面
	 **/
	@RequestMapping(value = {"create"})
	public ModelAndView create(HttpServletRequest request,HttpServletResponse response,${className}Model ${classNameLower}Model) throws Exception {
		return new ModelAndView("${classNameLower}Form","${classNameLower}",${classNameLower}Model);
	}
	
	/** 
	 * 保存新增对象
	 **/
	@RequestMapping(value = {"save"})
	public ModelAndView save(HttpServletRequest request,HttpServletResponse response,${className}Model ${classNameLower}Model) throws Exception {
		${classNameLower}Service.save(${classNameLower}Model);
		return new ModelAndView("${classNameLower}List");
	}
	
	/**
	 * 进入更新页面
	 **/
	@RequestMapping(value = {"edit"})
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response) throws Exception {
		<@generateIdParameter/>
		${className}Model ${classNameLower}Model = (${className}Model)${classNameLower}Service.getById(id);
		return new ModelAndView("${classNameLower}Form","${classNameLower}",${classNameLower}Model);
	}
	
	/**
	 * 保存更新对象
	 **/
	@RequestMapping(value = {"update"})
	public ModelAndView update(HttpServletRequest request,HttpServletResponse response) throws Exception {
		<@generateIdParameter/>
		
		${className}Model ${classNameLower}Model = (${className}Model)${classNameLower}Service.getById(id);
		Map params = WebUtils.getParametersStartingWith(request, "");
		BeanUtilsBean beanUtils = new BeanUtilsBean();
		beanUtils.copyProperties(${classNameLower}Model, params);
		${classNameLower}Service.update(${classNameLower}Model);
		return new ModelAndView("${classNameLower}List");
	}
	
	/**
	 *删除对象
	 **/
	@RequestMapping(value = {"delete"})
	public ModelAndView delete(HttpServletRequest request,HttpServletResponse response) {
		String[] ids = request.getParameterValues("ids");
		for(int i = 0; i < ids.length; i++) {
			${classNameLower}Service.deleteById(ids[i]);
		}
		return new ModelAndView("${classNameLower}List");
	}

}

<#macro generateIdParameter>
<#if table.keyJavaType="java.util.HashMap">
	HashMap id = new HashMap();
	<#list table.keyColumns as column>
		id.put("${column.classColumnName}",request.getParameter("${column.classColumnName}"));
	</#list>
<#else>
	<#list table.keyColumns as column>
		${table.keyJavaType} id = new ${table.keyJavaType}(request.getParameter("${column.classColumnName}"));
	</#list>
</#if>
</#macro>